#!/bin/sh


#    Copyright (c) 1996-2004 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Description:
#      Verification of an install for a particular platform.
#


usage() {
    program=`basename $0`

    echo "Usage: $program <platform> <TEST | EXTENDED> package"
    echo "  platform      <Optional> hardware must be this platform."
    echo "  TEST          Perofrm read/write testing of compact flash memory."
    echo "  EXTENDED      Perform extended testing of compact flash memory."
    echo "  package       Package to install."

    echo ""
    echo "Platform \"$PLATFORM\" packages are:"
    ls ./$PLATFORM/*.plist 2>/dev/null | cut -d"/" -f3 | cut -d "." -f1
    exit 1
}

#
# exit_out
#
exit_out()
{
    if [ $error = TRUE ]
    then
	echo "SCRIPT: verify FAILED with $warn_count warnings."
	exit 1
    else
	if [ $warn_count -gt 0 ]
	then
	    echo "SCRIPT: verify PASSED with $warn_count warnings."
	else
	    echo "SCRIPT: verfiy PASSED."
	fi
	exit 0
    fi
}

#
# do_command <warn/error> <command...>
#
do_command()
{
    warn=$1
    shift
    $*
    if [ $? -ne 0 ]
    then
	if [ $warn = TRUE ]
	then
	    echo "SCRIPT: Warning - failed $*"
	    warn_count=`expr $warn_count + 1`
	    return 1
	else
	    echo "SCRIPT: ERROR - failed $*"
	    continue=FALSE
	    error=TRUE
	    exit_out
	fi
    fi
    return 0
}
#
# verify-rpms <plist> <root>
#
verifyrpms()
{
    /bin/rm -f /tmp/partition_error

    LIST=$PLATFORM/$1

    if [ ! -f $LIST ] || [ ! -r $LIST ]; then
	echo "SCRIPT: Cannot open/read package list to verify."
	exit 1
    fi

    if [ ! -z $2 ]; then
	ROOT=$2
    else
        ROOT=/
    fi

    RPM="rpm  -V -p --root $ROOT"

    echo "SCRIPT: Verifying  RPMs from $LIST rooted at $ROOT..."

    for package in `cat $LIST | dd skip=18 bs=16 2> /dev/null | grep -v '#' | xargs`;
    do
	    result="`$RPM $package | \
			grep -e '5' -e 'missing' -e 'failed' | \
			grep -v ' c ' | \
			grep -v '/etc/inittab' | \
			grep -v '/etc/modules.conf' | \
			grep -v '/etc/rc.d/rc.pwdinit' | \
			grep -v '/etc/idle.conf' | \
			grep -v '/fabos/share/SWBD' | \
			grep -v '/etc/services' | \
			grep -v '/etc/raslog.conf' | \
			grep -v '/etc/config/webtools'`"
	    if [ ! -z "$result" ]
	    then
		$RPM $package | \
			grep -e '5' -e 'missing' -e 'failed' | \
			grep -v ' c ' | \
			grep -v '/etc/inittab' | \
			grep -v '/etc/modules.conf' | \
			grep -v '/etc/rc.d/rc.pwdinit' | \
			grep -v '/etc/idle.conf' | \
			grep -v '/fabos/share/SWBD' | \
			grep -v '/etc/services' | \
			grep -v '/etc/raslog.conf' | \
			grep -v '/etc/config/webtools' | \
			while read line
		do
			file=`echo "$line" | cut -d"/" -f3-`
			if [ ! -f ./$PLATFORM/Mfgtests/$file ]
			then
			    echo ""
			    echo "FAILED verify of rpm ${DEVICE}${partition} $package because of:"
			    echo $line
			    touch /tmp/partition_error
			fi
		done
	    else
		echo -n "."
#		echo "passed verify of rpm ${DEVICE}${partition} $package"
	    fi
    done
    if [ -f /tmp/partition_error ]
    then
	partition_error=1
	/bin/rm -f /tmp/partition_error
    fi
}

#
# test_for_package <candidate>
#
test_for_package()
{
    abc=`echo $1 | tr -s '[:upper:]' '[:lower:]'`

    #
    # See if first parameter was a PACKAGE
    #
    if [ -f ./$PLATFORM/$abc.plist ]
    then
	echo $abc
    else
	echo FALSE
    fi
}
#
# test_for_test <candidate>
#
test_for_test()
{
    if [ "$1" = "TEST" ]
    then
	echo 1
    else
	if [ "$1" = "EXTENDED" ]
	then
	    echo 2
	else
	    echo FALSE
	fi
    fi
}

#
# parse the command line arguments
#
parse_arguments()
{
    # check explicitly for package and test parameters, and assume 
    # anything else is the plaftorm validation
    #
    while [ $# -gt 0 ]
    do
	result=`test_for_package $1`
	if [ "$result" = "FALSE" ]
	then
	    result=`test_for_test $1`
	    if [ "$result" = "FALSE" ]
	    then
		#
		# Look up the name in the list of known names for the platform ID
		#
		TARGET_PLATFORM=`echo $1 | tr "A-Z" "a-z"`
		target=`grep $TARGET_PLATFORM platform_names`
		if [ "$target" != "" ]
		then
		    TARGET_PLATFORM=`grep $TARGET_PLATFORM platform_names | cut -d":" -f1`
		fi
		if [ -z $PLATFORM ]
		then
		    if [ ! -d ./$TARGET_PLATFORM ]
		    then
			echo "SCRIPT: Unknown Platform specified $TARGET_PLATFORM"
			usage
		    fi
		    PLATFORM=$TARGET_PLATFORM
		    case $PLATFORM in
			"SWBD10" ) REVISION=0;;
			"SWBD12" ) REVISION=3;;
			"SWBD21" ) REVISION=0;;
			"SWBD22" ) REVISION=0;;
			"SWBD26" ) REVISION=0;;
			"SWBD27" ) REVISION=0;;
			"SWBD28" ) REVISION=0;;
			"SWBD30" ) REVISION=0;;
			"SWBD31" ) REVISION=0;;
			*) REVISION=0;;
		    esac
		fi
	    else
		do_test=$result
	    fi
	else
	    PACKAGE=$result
	fi
	shift
    done
}

#------------------------------
# BEGIN MAIN PART OF THE SCRIPT
# BEGIN MAIN PART OF THE SCRIPT
#------------------------------

if [ -z `pwd | grep load` ]
then
    echo "SCRIPT: ERROR verification directory is not mounted on /load."
    exit 1
fi

for mnt in `mount | cut -d" " -f3 | grep "/mnt"`
do
    umount $mnt
done

FSOPTS='-f'
DEVICE='hda'
TMPMNT='/mnt'

warn_count=0
continue=TRUE
error=FALSE

PLATFORM=`sin | \
		  /bin/grep Platform | \
		  /usr/bin/cut -d" " -f2 | \
		  /usr/bin/cut -d"," -f1`

REVISION=`sin | \
		  /bin/grep Platform | \
		  /usr/bin/cut -d" " -f4`

TARGET_PLATFORM=$PLATFORM
######################
# parse the arguments
#
parse_arguments $*

XFS=FALSE
case "$PLATFORM" in
    "SWBD10" )
	XFS=TRUE
	;;
    "SWBD12" ) 
	case $REVISION in
	    1|2|3) XFS=TRUE;;
	esac
	;;
     "SWBD21" )
	LDR2_MTD=mtd6
	PD_MTD=mtd4
	;;
     "SWBD26" | "SWBD27" )
	FS=ext3
	;;
    * ) XFS=FALSE;;
esac

#######################
# Check the parameters
#
if [ ! -f ./$PLATFORM/$PACKAGE.plist ]
then
    printf "SCRIPT: Incorrect package %s\n" $PACKAGE
    usage
fi
if [ $PLATFORM != $TARGET_PLATFORM ]
then
    echo "SCRIPT: Actual platform ($PLATFORM) does not match specified platform $TARGET_PLATFORM."
    echo "SCRIPT: ERROR - failed $0 $*"
    continue=FALSE
    error=TRUE
    exit_out
fi

##############################################
# Let's get down to it and install this thing
#
echo "SCRIPT: VERIFYING installation for platform: $PLATFORM revision: $REVISION package $PACKAGE"

for partition in 1 2; do
  if [ $continue = TRUE ]
  then
    echo "SCRIPT: Checking file system on /dev/${DEVICE}${partition}..."

    do_command FALSE mount /dev/${DEVICE}${partition} ${TMPMNT}

    partition_error=0
    verifyrpms $PACKAGE.plist ${TMPMNT}
    echo ""
    if [ $partition_error -ne 0 ]
    then
	echo "SCRIPT: verification FAILED on partition ${DEVICE}${partition}"
	error=TRUE
    else
	echo "SCRIPT: verification PASSED on partition ${DEVICE}${partition}"
    fi
    echo "SCRIPT: Unmounting file system..."

    do_command FALSE umount ${TMPMNT}
  fi
done

exit_out
